package com.proxy.dynamic_proxy_cglib;

import com.proxy.dynamic_proxy_jdk.Rent;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 项目名称: java_algorithm
 * @ClassName CglibDynamicProxy
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/5/30 14:08
 */
public class CglibDynamicProxy implements MethodInterceptor {

    private Teacher teacher;

    public CglibDynamicProxy(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("方法增强钱");
        Object invoke = method.invoke(o, objects);
        System.out.println("方法增强后");
        return null;
    }

    public Object getProxy(){
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(teacher.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
}
